package com.omesoft.hksnore.util.snore;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.omesoft.hksnore.HMInfo;
import com.omesoft.hksnore.util.photo.SettingUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * Created by Omesoft on 2017/3/5.
 * <p>
 * 鼾声监测 ，查看历史记录
 */

public class SnoreMonitorHis extends Thread {
    // private int TSnoreMin = 600;// 鼾声时长最小值,毫秒数
    // private int TSnoreMax = 3000;// 鼾声时长最大值,毫秒数
    // private int TNoSnoreMin = 1400;// 鼾声时长最小值,毫秒数
    // private int TNoSnoreMax = 4000;// 鼾声时长最大值,毫秒数
    private int TSnoreRateMin = 2800;// 鼾声时长最小值,毫秒数
    private int TSnoreRateMax = 6000;// 鼾声时长最大值,毫秒数
    private int TTureMax = 4000;// 鼾声时长最大值,毫秒数
    private float SnoreCoefficient = 3.0f;// 差值系数
    private int SnoreCountCon = 5;// 连续N次鼾声
    private int VSnoren = 40;// 鼾声分贝大小
    // private int SnorePoint = 3;// 连续大于阀值的点
    private float compareTmpAvg = 18.0f;// 低点平均值--160127

    private long snoreStartTime = 0;// 鼾声开始时刻
    private long snoreStopTime = 0;// 鼾声结束时刻
    private long lastSnoreStartTime = 0;// 上一次鼾声开始时刻
    private long lastSnoreStopTime = 0;// 上一次鼾声结束时刻
    // private long lastSnoreTime = 0;// 上一次鼾声时长
    // private long snoreTime = 0;// 下次鼾声时长
    private long noSnoreTime = 0;// 下次鼾声开始时刻
    private List<Long> snoreCountForTempTime = new ArrayList<Long>();// 最近鼾声时刻

    // private int dbSumFor2min = 0;
    private int snoreCountForTemp = 0;// 鼾声次数缓存累计
    private int snoreCountAtNight = 0;// 鼾声次数累计
    private long waveCrestTime = 0;
    private float waveCrestValue = 0;
    private long lastWaveCrestTime = 0;
    private float lastWaveCrestValue = 0;
    // private long newActTime = 0;
    private long lastActTime = 0;
    private long snoreLastActTureTime = 0;
    private int snore2AllActCount = 0;
    private int actCountFor2min = 0;
    private int snore2AllTurnCount = 0;
    // float simpleMax = 0, simpleMin = 100;
    private int sampleCount = 0;
    private int samplePreSec = 5;
    private long waitTime = 0;
    private float lastCompareTmp = 0;
    private float lastDB = 0;
    private float waveSimpleAvg = 40;
    private float minSnoreSimpleAvg = 40;
    private List<Float> snoreDBTmpList = new ArrayList<Float>();
    private List<Long> snoreCurTimeTmpList = new ArrayList<Long>();
    private long recodeStartTime = 0;
    private boolean isRecodeStartTime = false;
    private long snoreAccountTime = 0;// 鼾声开始时刻
    private List<Float> compareTmpList = new ArrayList<Float>();
    // private float lastDbavMaxTmp = 0;
    private float breathRate = 16.0f;
    // ----
    private List<Float> showedList0 = new ArrayList<Float>();
    private List<Float> showedList01 = new ArrayList<Float>();
    private List<Float> showedList1 = new ArrayList<Float>();
    private List<Float> showedList11 = new ArrayList<Float>();
    private long drawTime = 0;// 鼾声开始时刻
    private int maxNum = 0;
    private String OverTime;

    private int snore10AllTurnCount = 0;// 10分钟翻身次数
    private List<HMInfo> allTurnCount = new ArrayList<HMInfo>();// 存放10分钟翻身次数的列表
    private long turnCountStartTime = 0;// 10分钟翻身开始的时间
    private int intervalTime = 300000;// 翻身次数间隔取样时长

    private long lastVBTime = 0;// 上一次振动的时间
    private int vbLv = 0;// 振动叠加等级
    private int vbLvCount = 0;// 止鼾次数

    private int snore5AllMoveCount = 0;//
    private List<HMInfo> allMove = new ArrayList<HMInfo>();// 存放10分钟体动次数的列表
    private long moveStartTime = 0;// 10分钟体动开始的时间
    private int moveDur = 600000;// 体动次数间隔取样时长

    // --------------------------------以下为历史记录的不同变量

    private int sleepTime = 50;// 线程等待时间
    private boolean isLoop = true;
    private Context context;
    private Handler handler0;
    private String fileName = "";
    private MappedByteBuffer m_mbBuf = null;
    private int m_mbBufLen = 0;
    private int m_mbBufEnd = 0;
    private Vector<String> m_lines = new Vector<String>();

    private boolean isPuse = false;
    private int fastMultiple = 1;
    private boolean isQuick = false;
    /**
     * 记录是否关闭当前activity
     */
    // private boolean isFastForward = false;
    private List<HMInfo> dataListY = new ArrayList<HMInfo>();

    private String fileNameLastTime = "";

    public SnoreMonitorHis(Context context, Handler handler0, String fileName) {
        this.context = context;
        this.handler0 = handler0;
        this.fileName = fileName;
    }

    @Override
    public void run() {
        isLoop = true;
        waitTime = 0;
        snoreAccountTime = 0;
        // 初始化文件TXT
        String[][] time = SettingUtil.ReadTxtFile(context, SettingUtil.TEXT_DIR + "/" + fileName);
        Log.e("fileName", "fileName: " + fileName);
        fileNameLastTime = time[0][0];//开始时间
        OverTime = time[0][2];
        sendMsg2(21, time[0][1]);// 总时间
        Log.e("xx", "fileNameLastTime " + fileNameLastTime);
        File book_file = new File(SettingUtil.TEXT_DIR + "/" + fileName);
        long lLen = book_file.length();
        m_mbBufLen = (int) lLen;// 文件总长度
        try {
            m_mbBuf = new RandomAccessFile(book_file, "r").getChannel().map(
                    FileChannel.MapMode.READ_ONLY, 0, lLen);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int i = 0;
        while (m_mbBufEnd <= m_mbBufLen && isLoop) {// 获取数据
            if (m_mbBufEnd >= m_mbBufLen) {
                // 休眠控制采样
                try {
                    // Log.d("sherlock", "回放结束" + sleepTime);
                    sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                continue;
            }

            try {
                /**
                 * 拖动进度条的时候会进入该判定
                 */
                if (SettingUtil.isFastForward || m_mbBufEnd == m_mbBufLen) {
                    continue;
                }
                nextPage();
                dataListY.clear();
                dataListY = new ArrayList<HMInfo>();
                dataListY = getDataList(m_lines);
                if (!SettingUtil.isMove) {

                    int progressD = (m_mbBufEnd * SettingUtil.MAXCOUNT2)
                            / m_mbBufLen;
                    sendMsg2(14, progressD);
                }
                i = 0;
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // Log.d("sherlock", "dataListY.size()::" + dataListY.size());

            SimpleDateFormat tempDateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");

            while (isLoop && i < dataListY.size()) {// 显示数据
                // Log.d("sherlock", "计算速度::" + System.currentTimeMillis());
                try {
                    // 休眠控制采样
                    // Log.d("sherlock", "休眠控制" + sleepTime);
                    if (isQuick) {
                        sleep(0);
                    } else {
                        sleep(sleepTime / fastMultiple);
                    }
                    // 如果暂停
                    if (isPuse) {
                        sleep(50);
                        continue;
                    }
                    if (SettingUtil.isFastForward) {
                        break;
                    }
                    // 发送采样频率
                    sampleCount++;

                    if (SettingUtil.isDateStringValid(dataListY.get(i)
                            .gettTime())) {
                        // 获取时间和分贝值
                        String currentTime = dataListY.get(i).gettTime();
                        long curTime = tempDateFormat.parse(currentTime)
                                .getTime();
                        float curdB = dataListY.get(i).gettValue();// 转换为响度，dB

                        if (!isRecodeStartTime) {
                            isRecodeStartTime = true;
                            recodeStartTime = curTime;
                            turnCountStartTime = curTime;
                            moveStartTime = curTime;

                        }
                        String testTime = SettingUtil.getTestTime(context, currentTime, fileNameLastTime);//当前进度时间减去监测开始时间
                        sendMsg2(20, testTime);
                        if (curTime - waitTime >= 2000) {

                            waitTime = curTime;
                            if (sampleCount > 1) {
                                samplePreSec = sampleCount / 3;
                                sleepTime = 2000 / sampleCount;

                            }
                            sampleCount = 0;
                        }
                        // jiange++;// 如果点数过多，用来做采样过滤
                        // if(jiange%3==1)
                        Log.e("curdB", "run: curdB="+curdB);

                        try {
                            if (curdB >= 0 && curdB < 120) {
                                // 对DB的值保留两位小数
                                float dB = Math.round(curdB * 10) / 10.0f;
                                avgData(curTime, dB);// 处理画线和计算数据

                                if (System.currentTimeMillis() - drawTime > 200) {
                                    drawTime = System.currentTimeMillis();
                                    sendMsg2(0, showedList0);
                                    sendMsg2(1, showedList01);
                                    sendMsg2(10, showedList01);
//                                    sendMsg2(18, showedList11);
//                                    Log.e("sendMsg2", "run: "+"showedList0="+showedList0.size()+"   showedList01="+showedList01.size()+"   showedList1="+showedList1.size()+"   showedList11="+ showedList11.size() );

                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    i++;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            // Log.e("lg", "翻身次数:::" + allTurnCount.toString());
        }
    }

    /**
     * 传原始值和时间，处理平滑曲线
     *
     * @param curTime
     * @param
     */
    private void avgData(long curTime, float dB) {
        // 加入缓存LIST
        snoreDBTmpList.add(dB);
        snoreCurTimeTmpList.add(curTime);
        // 平滑点数，以每秒采样来处理
        // samplePreSec = 6;
        if (snoreDBTmpList.size() > samplePreSec) {
            float dbAvg = 0;
            float dbSum = 0;
            float dbMax = 0;
            float calcPs = samplePreSec * 1.0f;
            // Log.d("sherlock", "calcPs::" + calcPs);
            for (int j = 0; j < snoreDBTmpList.size(); j++) {
                dbSum += snoreDBTmpList.get(j);
                dbMax = Math.max(dbMax, snoreDBTmpList.get(j));
            }
            dbAvg = dbSum / snoreDBTmpList.size() * 0.8f + dbMax * 0.2f;
            dbAvg = Math.round(dbAvg * 10) / 10.0f;
            minSnoreSimpleAvg = Math.min(minSnoreSimpleAvg, dbAvg)
                    + SnoreCoefficient * 0.05f / calcPs;
            waveSimpleAvg = waveSimpleAvg * 0.95f + minSnoreSimpleAvg * 0.05f;
            // Log.d("sherlock", "minDBAvg::" + snoreSimpleAvg);
            synchronized (showedList0) {
                showedList0.add(snoreDBTmpList.get(snoreDBTmpList.size() / 2));
                if (showedList0.size() > 50 && showedList0.size() > maxNum) {
                    showedList0.remove(0);
                }
            }
            synchronized (showedList01) {
                showedList01.add((float) dbAvg);
                if (showedList01.size() > 50 && showedList01.size() > maxNum) {
                    showedList01.remove(0);
                }
            }
            clacSnore151016(
                    snoreCurTimeTmpList.get(snoreCurTimeTmpList.size() / 2),
                    dbAvg);

        }
        // 把多余点去掉
        while (snoreDBTmpList.size() > samplePreSec) {
            snoreDBTmpList.remove(0);
            snoreCurTimeTmpList.remove(0);
        }
    }

    /**
     * 计算鼾声算法
     *
     * @param
     * @param curTime
     */
    private void clacSnore151016(long curTime, float dbAv) {
        // 1.连续上升或者下降2.间隔时长，3.连续3个波峰
        if (dbAv >= 0 && dbAv < 120) {// 防止负无穷数据影响
            // 累计波峰点
            // 累计鼾声的时间
            // 计算实时响度与均值的差值，根据差值的大小确定判断动作
            float compareTmp = dbAv - waveSimpleAvg;
            Log.d("sherlock", "compareTmp::" + compareTmp
                    + "--SnoreCoefficient::" + SnoreCoefficient);
            if (compareTmp > SnoreCoefficient) {
                compareTmpList.add(compareTmp);
//                 lastDbavMaxTmp = Math.max(lastDbavMaxTmp, dbAv);

                if (waveCrestValue < dbAv) {
                    waveCrestValue = dbAv;
                    waveCrestTime = curTime;
                }
                if (compareTmpList.size() == 1) {
                    // 第一个符合条件点，记录波动开始时间
                    snoreStartTime = curTime;
                }

            } else {
                // 计算低点平均值
                compareTmpAvg = compareTmpAvg * 0.95f + dbAv * 0.05f;
                Log.d("sherlock", "compareTmpAvg::" + compareTmpAvg);
                if (compareTmpAvg < 20) {
                    VSnoren = 25;
                    SnoreCoefficient = 9.0f;
                } else if (compareTmpAvg < 30) {
                    VSnoren = 35;
                    SnoreCoefficient = 7.0f;
                } else if (compareTmpAvg < 40) {
                    VSnoren = 45;
                    SnoreCoefficient = 5.0f;
                } else if (compareTmpAvg < 50) {
                    VSnoren = 50;
                    SnoreCoefficient = 3.0f;
                } else {
                    VSnoren = 55;
                    SnoreCoefficient = 1.0f;
                }

                if (lastCompareTmp > SnoreCoefficient) {
                    // 计算鼾声边界值
                    // 第一个不符合条件点，记录波动结束时间
                    snoreStopTime = curTime;
                    // long snoreTime = snoreStopTime - snoreStartTime;
                    long snoreTime = snoreStopTime - snoreStartTime;
                    // long snoreCycleTime = snoreStopTime -
                    // lastSnoreStopTime;// 波动周期时长
                    long snoreCycleTime = waveCrestTime - lastWaveCrestTime;// 波动周期时长
                    Log.d("sherlock", "波峰" + compareTmpList.size() + "个点::"
                            + "时长::" + snoreTime + "周期时长::" + snoreCycleTime);
                    // snoreTime < TSnoreMax && snoreTime > TSnoreMin&&
                    // 判断一个波动周期是否符合范围
                    if (snoreCycleTime > TSnoreRateMin
                            && snoreCycleTime < TSnoreRateMax) {
                        // 清零次数
                        if (snoreCountForTempTime.size() > 0
                                && snoreStopTime
                                - snoreCountForTempTime
                                .get(snoreCountForTempTime
                                        .size() - 1) > TSnoreRateMax) {
                            snoreCountForTemp = 0;
                            snoreCountForTempTime.clear();
                        }
                        snoreCountForTemp++;// 缓存鼾声次数+1
                        snoreCountForTempTime.add(snoreStopTime);
                        Log.d("sherlock", "波形符合::" + snoreCountForTemp);

                        // 只要最近N个鼾声的，多出的就去掉
                        if (snoreCountForTempTime.size() > SnoreCountCon + 1) {
                            snoreCountForTempTime.remove(0);
                        }
                        // 判断N个波动周期是否符合范围
                        long snoreNTime = snoreStopTime
                                - snoreCountForTempTime.get(0);
                        if (snoreCountForTemp >= SnoreCountCon) {
                            if (snoreNTime > (TSnoreRateMin) * SnoreCountCon
                                    && snoreNTime < (TSnoreRateMax)
                                    * SnoreCountCon) {
                                // Log.d("sherlock", "多个波形时长也符合::"
                                // + snoreCountForTemp);
                                startRecord();

                                if (waveCrestValue > VSnoren) {// 鼾声要满足条件，最高声音
                                    if (snoreCountForTemp == SnoreCountCon) {// 如果是第一次满足次数则多加几个
                                        snoreCountAtNight += SnoreCountCon;
                                        snoreAccountTime += curTime
                                                - snoreCountForTempTime.get(0);
                                    } else {
                                        snoreCountAtNight++;
                                        snoreAccountTime += snoreStopTime
                                                - snoreCountForTempTime
                                                .get(snoreCountForTempTime
                                                        .size() - 2);
                                    }
                                    // 处理振动等级
                                    long durTime = curTime - lastVBTime;
                                    if (durTime > 600000) {
                                        vbLv = 0;
                                    } else {
                                        vbLv++;
                                    }
                                    if (durTime > 25000) {
                                        vbLvCount++;
//                                         sendMsg2(22, vbLv);// 发送振动通知
                                        sendMsg2(23, vbLvCount);// 发送止鼾次数
                                        lastVBTime = curTime;
                                    } else {
                                    }
                                } else {
                                    Log.d("sherlock", "声音太小::" + waveCrestValue);
                                }
                                float breathRateTmp = 60000 * SnoreCountCon / snoreNTime;
                                breathRateTmp = Math.max(breathRateTmp, 12);
                                breathRateTmp = Math.min(breathRateTmp, 20);
                                breathRate = (float) (breathRate * 0.95 + breathRateTmp * 0.05);
                                Log.d("sherlock", "breathRateTmp::"
                                        + breathRate);
                                // 发送鼾声次数
                                sendMsg2(11, snoreCountAtNight);
                                // 发送鼾声时长
                                sendMsg2(12, Math.round(snoreAccountTime / 6000) / 10.0);
                                // 发送鼾声频率
                                // sendMsg2(
                                // 15,
                                // Math.round(snoreCountFor2min * 60000
                                // / snoreAccountTime));
                                sendMsg2(15, Math.round(breathRate));// 最近几次的平均频率
                                lastSnoreStopTime = curTime;
                            } else {
                                Log.d("sherlock", "N个鼾声时长不符合::" + snoreNTime);
                            }
                        } else {
                            Log.d("sherlock", "波形次数不够::" + snoreCountForTemp);
                        }
                    } else {
                        // 清零鼾声缓存数据
                        snoreCountForTemp = 0;
                        snoreCountForTempTime.clear();

                        // 计算动作
                        if (curTime - lastActTime > 5000) {
                            actCountFor2min++;
                            snore2AllActCount++;
                            snore5AllMoveCount++;
                            sendMsg2(16, snore2AllActCount);
                        }
                        // 计算翻身
                        if (snoreTime > TTureMax) {
                            if (curTime - snoreLastActTureTime > 10000) {
                                snore10AllTurnCount++;
                                snore2AllTurnCount++;
                                sendMsg2(17, snore2AllTurnCount);
                            }
                            snoreLastActTureTime = curTime;
                        }
                        lastActTime = curTime;
                    }

                    lastSnoreStartTime = snoreStartTime;
                    lastSnoreStopTime = snoreStopTime;
                    lastWaveCrestTime = waveCrestTime;
                    lastWaveCrestValue = waveCrestValue;
                } else {

                }
                if (curTime - lastSnoreStopTime > 60000) {// 间隔时长过长，停止录音
                    stopRecord();
                }
                calcMove(curTime);
                // trunCountBySleepDur(curTime);
                // 遇到不符合的点，则清空LIST
                compareTmpList.clear();
                // lastDbavMaxTmp = 0;
                waveCrestTime = 0;
                waveCrestValue = 0;

            }

            lastCompareTmp = compareTmp;
            compareTmp = Math.round(compareTmp * 10) / 10.00f;

            showedList1
                    .add((float) ((compareTmp > SnoreCoefficient) ? compareTmp
                            : 0));
            if (showedList1.size() > 50 && showedList1.size() > maxNum) {
                showedList1.remove(0);
            }

        } else {
            Log.d("sherlock", "声音异常");
        }
    }

    /**
     * 处理体动的曲线 5分钟记录一次
     */
    private void calcMove(long curTime) {
        if (curTime - moveStartTime >= moveDur) {
            moveStartTime = curTime;

            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            String timeName = "";
            timeName = dateFormat.format(curTime);
            HMInfo hi = new HMInfo();
            hi.settTime(timeName);
            hi.settValue(snore5AllMoveCount);
            allMove.add(hi);
            Log.e("lg", "体动次数::" + allMove.size());
            snore5AllMoveCount = snore5AllMoveCount;
            showedList11.add((float) snore5AllMoveCount);
            if (showedList11.size() > 50 && showedList11.size() > maxNum) {
                showedList11.remove(0);
            }
            showedList11.add((float) snore5AllMoveCount);
            if (showedList11.size() > 50 && showedList11.size() > maxNum) {
                showedList11.remove(0);
            }
            snore5AllMoveCount = 0;
        }
    }

    // private void trunCountBySleepDur(long curTime) {
    // if (curTime - turnCountStartTime >= intervalTime) {
    // turnCountStartTime = curTime;
    //
    // SimpleDateFormat dateFormat = new
    // SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    // String timeName = "";
    // // if(allTurnCount.size()>0&allTurnCount.size()>SettingUtil.maxNum){
    // // allTurnCount.remove(0);
    // // }
    // timeName = dateFormat.format(curTime);
    // HMInfo hi = new HMInfo();
    // hi.settTime(timeName);
    // hi.settValue(snore10AllTurnCount);
    // allTurnCount.add(hi);
    // Log.e("lg", "翻身次数::" + allTurnCount.size());
    //
    // showedList11.add((float) snore10AllTurnCount * SettingUtil.num);
    // if (showedList11.size() > 50 && showedList11.size() > maxNum) {
    // showedList11.remove(0);
    // }
    //
    // snore10AllTurnCount = 0;
    // }
    // }

    public void nextPage() throws IOException {
        m_lines.clear();
        m_lines = pageDown();
    }

    private Vector<String> pageDown() {
        if (m_mbBufEnd < 0) {
            m_mbBufEnd = 0;
        }
//        Log.i("m_mbBufEnd1----", m_mbBufEnd + "");

        String strParagraph = "";
        Vector<String> lines = new Vector<String>();
        while (lines.size() < SettingUtil.mLineCount && m_mbBufEnd < m_mbBufLen) {
            byte[] paraBuf = readDataForward(); // 读取一个段落
            m_mbBufEnd += paraBuf.length;
            try {
                strParagraph = new String(paraBuf, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String strReturn = "";
            if (strParagraph.indexOf("\r\n") != -1) {
                strReturn = "\r\n";
                strParagraph = strParagraph.replaceAll("\r\n", "");
            } else if (strParagraph.indexOf("\n") != -1) {
                strReturn = "\n";
                strParagraph = strParagraph.replaceAll("\n", "");
            }

            if (strParagraph.length() == 0) {
                lines.add(strParagraph);
            }
            while (strParagraph.length() > 0) {
                // Log.e("WT", "strParagraph:::" + strParagraph);
                lines.add(strParagraph.substring(0, strParagraph.length()));

                strParagraph = strParagraph.substring(strParagraph.length());
                if (lines.size() >= SettingUtil.mLineCount) {
                    break;
                }
            }
            if (strParagraph.length() != 0) {
                try {
                    m_mbBufEnd -= (strParagraph + strReturn).getBytes("UTF-8").length;
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        /**
         * 处理 拿错了数据 例如 数据拿到这样子 11:12:12:211,122
         */
        Vector<String> lines2 = new Vector<String>();
        for (String line : lines) {
            if (line.length() > 22) {
                lines2.add(line);
            }
        }
        return lines2;
    }

    private byte[] readDataForward() {
        int nStart = m_mbBufEnd;
        Log.i("m_mbBufEnd1----", m_mbBufEnd + "readDataForward");
        int i = nStart;
        byte b0;
        // 根据编码格式判断换行
        while (i < m_mbBufLen) {
            b0 = m_mbBuf.get(i++);
            if (b0 == 0x0a) {
                break;
            }
        }
        int nParaSize = i - nStart;
        byte[] buf = new byte[nParaSize];
        for (i = 0; i < nParaSize; i++) {
            buf[i] = m_mbBuf.get(m_mbBufEnd + i);

        }
        return buf;
    }

    private List<HMInfo> getDataList(Vector<String> lines) {

        List<HMInfo> tempdataList = new ArrayList<HMInfo>();
        if (lines.size() > 0) {

            for (int i = 0; i < lines.size(); i++) {

                if (lines.get(i).length() > 0) {
                    String[] str1 = lines.get(i).split(",");
                    for (int j = 1; j < str1.length; j++) {
                        HMInfo h = new HMInfo();
                        h.settTime(str1[0]);
                        h.settValue(Float.parseFloat(str1[j]));
                        tempdataList.add(h);
                    }

                }

            }
        }
        return tempdataList;
    }

    public void toDataPos(int currentProgress) {
        float progressD = ((float) currentProgress / SettingUtil.MAXCOUNT2)
                * SettingUtil.MAXCOUNT2;
        // 跳转进度
        Log.e("WT", "currentProgress:::" + currentProgress);
        Log.e("WT", "progressD:::" + progressD);
        Log.e("WT", "文件长度m_mbBufLen1:::" + m_mbBufLen);
        Log.e("WT", "当前文件进度:::"
                + (int) (m_mbBufLen * progressD / SettingUtil.MAXCOUNT2));

        isPuse = false;
        m_mbBufEnd = (int) (m_mbBufLen * progressD / SettingUtil.MAXCOUNT2);
        SettingUtil.isFastForward = false;
    }

    // private void sendMsg(int w, Object v) {
    // if (!isQuick) {// 若是时间过小则表示是快速计算
    // Message msg = new Message();
    // msg.what = w;
    // msg.obj = v;// 响度平滑线
    // // msg.obj = compareTmp > 10 ? 10 : compareTmp;
    // handler0.sendMessage(msg);
    // }
    // }

    private void sendMsg2(int w, Object v) {
        Message msg = new Message();
        msg.what = w;
        msg.obj = v;// 响度平滑线
        // msg.obj = compareTmp > 10 ? 10 : compareTmp;
        handler0.sendMessage(msg);
    }

    private void startRecord() {
    }

    /**
     * 停止录音
     */
    private void stopRecord() {

    }

    public void monitorStop() {
        if (isLoop) {
            isLoop = false;
        }
    }

    public void monitorPuse(boolean isPuse) {
        this.isPuse = isPuse;
    }

    public void monitorDestory() {
        monitorStop();
    }

    public int getSleepTime() {
        return sleepTime;
    }

    public void setSleepTime(int sleepTime) {
        this.sleepTime = sleepTime;
    }

    public int getFastMultiple() {
        return fastMultiple;
    }

    public int setFastMultiple() {
        if (fastMultiple == 0) {
            fastMultiple += 1;
        } else {
            fastMultiple = fastMultiple * 2;
            if (fastMultiple > 8) {
                fastMultiple = 0;
            }
        }
        return fastMultiple;
    }
    public boolean getQuick() {
        return isQuick;
    }

    public void setQuick(boolean isQuick) {
        this.isQuick = isQuick;
    }

    public int getMaxNum() {
        return maxNum;
    }

    public void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }
}
